package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查询购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //查询当前登陆用户的购物车
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //根据创建时间正序排序
        lqw.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> shoppingCarts = shoppingCartService.list(lqw);

        return R.success(shoppingCarts);
    }

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //1. 判断一下当前添加的菜品或者套餐在 当前登陆用的购物车中是否存在
        //1.1 根据当前登陆用户id和菜品id或者套餐id查询
        //select * from shopping_cart where user_id = 1 and dish_id = 22312/setmeal_id=12412412
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
        //如果前端传递过来的菜品id不为空，就根据菜品id查询
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        //如果前端传递过来的套餐id不为空，就根据套餐id查询
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        ShoppingCart shoppingCartDo = shoppingCartService.getOne(lqw);
        if (shoppingCartDo != null) {
            //2. 如果存在(当前商品在购物车中存在)？数量+1
            shoppingCartDo.setNumber(shoppingCartDo.getNumber() + 1);
            shoppingCartService.updateById(shoppingCartDo);
        } else {
            //3. 如果不存在，加入购物车，数量默认为1
            shoppingCart.setUserId(BaseContext.getCurrentId());
//            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCartDo = shoppingCart;
        }
        return R.success(shoppingCartDo);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //清空购物车
        //delete from shopping_cart where user_id = ?
        shoppingCartService.remove(lqw);
        return R.success("清空购物车成功");
    }

    /**
     * 购物车的商品减少功能
     *
     * @return
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        //登录用户的id
        Long currentId = BaseContext.getCurrentId();
        //构建条件查询对应的购物车的食物的数量
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart cart = shoppingCartService.getOne(lqw);

        //如果数量大于1,则减一,否则删除
        Integer number = cart.getNumber();
        if (number > 1) {
            cart.setNumber(number - 1);
            shoppingCartService.updateById(cart);
            return R.success("删除成功");
        }
        shoppingCartService.removeById(cart);
        return R.success("删除成功");
    }
}